package com.nervenets.shopping.controller.role.params;

import com.nervenets.general.Global;
import com.nervenets.general.web.params.FormParams;
import com.nervenets.shopping.hibernate.domain.user.RoleGroup;
import com.nervenets.shopping.service.RoleGroupService;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import lombok.experimental.SuperBuilder;
import org.springframework.beans.BeanUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

@EqualsAndHashCode(callSuper = true)
@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
@SuperBuilder
public class RoleGroupEditParams extends FormParams<RoleGroup, RoleGroupService> {
    @ApiModelProperty(value = "权限组名称", required = true)
    @NotNull(message = "权限组名称不能为空")
    private String name;
    @ApiModelProperty(value = "权限组描述", required = true)
    @NotNull(message = "权限组描述不能为空")
    private String detail;//描述
    @ApiModelProperty(value = "权限组权限明细", required = true)
    @NotEmpty(message = "权限组权限明细不能为空")
    private List<String> roles;
    @ApiModelProperty(value = "是否启用")
    private boolean enable;
    @ApiModelProperty(value = "排序值，值越大越靠前")
    private int queue;
    @ApiModelProperty(value = "权限父级ID，顶级传0")
    private long parentId;
    @ApiModelProperty(value = "用户IDs")
    private List<Long> userIds;

    @Override
    public RoleGroup validateAndBuild(RoleGroupService service, HttpServletRequest request, HttpServletResponse response) {
        RoleGroup roleGroup = RoleGroup.builder().build();
        if (getFormId() > 0) {
            roleGroup = service.findOne(getFormId());
        }

        BeanUtils.copyProperties(this, roleGroup, Global.defaultIgnoreProperties());

        return roleGroup;
    }
}
